﻿Imports TMySQL
Imports ZedGraph
Public Class frmPrefixIndex
    Private _Table As IHasColumns
    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
    End Sub
    Public Sub New(ByVal ATable As IHasColumns)
        Me.New()
        _Table = ATable
        InitCbo()
        txtPrefixLength.Text = "10"
        Graph.GraphPane.Title.Text = String.Empty
    End Sub
    Private Sub InitCbo()
        cboColumn.Items.Clear()
        For i As Integer = 0 To _Table.ColumnCount - 1
            If _Table.Columns(i).IsString Then
                cboColumn.Items.Add(_Table.Columns(i).ColumnName)
            End If
        Next
        If cboColumn.Items.Count > 0 Then
            cboColumn.SelectedIndex = 0
        End If
    End Sub

    Private Sub cmdExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExecute.Click
        If cboColumn.SelectedIndex < 0 Then
            Return
        End If
        If MsgBox("少々時間がかかりますけどよろしいですか？", MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            Cursor.Current = Cursors.WaitCursor
            Try
                ShowResult(cboColumn.SelectedItem)
            Finally
                Cursor.Current = Cursors.Default
            End Try
        End If
    End Sub

    Private Sub txtPrefixLength_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrefixLength.KeyPress
        If e.KeyChar < "0"c OrElse e.KeyChar > "9"c Then
            e.Handled = True
        End If
    End Sub

    Private Sub ShowResult(ByVal AColumnName As String)
        Dim GraphPane As GraphPane = Graph.GraphPane
        GraphPane.CurveList.Clear()
        If _Table.GetRowCount <= 0 Then
            Return
        End If
        _Table.PrefixLength = Common.StrToIntDef(txtPrefixLength.Text, 10)
        Dim dt As DataTable = _Table.GetCardinarySet(AColumnName)
        Grid.DataSource = dt

        Dim list1 As PointPairList = New PointPairList()
        For i As Integer = 0 To _Table.PrefixLength
            list1.Add(i, dt(i)("value"))
        Next
        GraphPane.AddCurve("Prefix", list1, Color.Red, SymbolType.Diamond)
        Graph.AxisChange()
        Graph.Invalidate()
    End Sub
End Class